﻿using System;
using System.Windows.Forms;
using System.Xml;

namespace internetmanage.jiemian
{
    public partial class Form_xml : Form
    {
        public Form_xml()
        {
            InitializeComponent();
        }

        private void Form_xml_Load(object sender, EventArgs e)
        {
            this.richTextBox1.Text = stringxml;
        }

        public string stringxml;

        private void button1_Click(object sender, EventArgs e)
        {
            readXml();

          

        }

        public void readXml()
        {
            try
            {
                this.treeView1.Nodes.Clear();

                XmlDocument document =
                new XmlDocument();
                document.LoadXml(this.richTextBox1.Text);
                populateTreeControl(document, treeView1.Nodes);

                this.treeView1.ExpandAll();

            }
            catch (Exception)
            {

                
            }
           
            //XmlDocument doc;

            //document.Load(@"C:\simple\mysoft\internetmanage\分类\other\bpmn\flowable\simple.bpmn20.xml");

            //foreach (XmlNode xn in document.ChildNodes)
            //{
            //    if (xn.NodeType == XmlNodeType.XmlDeclaration)
            //    {
            //        string text = xn.OuterXml;
            //        TreeNode new_child = new TreeNode(text);
            //        this.treeView1.Nodes.Add(new_child);
            //    }
            //    else {
            //        string text = xn.Name;
            //        TreeNode new_child = new TreeNode(text);
            //        this.treeView1.Nodes.Add(new_child);
            //    }

            //}

            //string text = document.FirstChild.InnerText.ToString();
            //TreeNode new_child = new TreeNode(text);
            //this.treeView1.Nodes.Add(new_child);

            //populateTreeControl(document.DocumentElement,
            //new_child.Nodes);
        }

        private void populateTreeControl(XmlNode document, TreeNodeCollection nodes)
        {
            foreach (XmlNode node in document.ChildNodes)
            {
                // If the element has a value, display it;
                // otherwise display the first attribute
                // (if there is one) or the element name
                // (if there isn't)
                TreeNode new_child = new TreeNode();
                if (node.NodeType == XmlNodeType.XmlDeclaration)
                {
                    string text = node.OuterXml;
                    new_child = new TreeNode(text);
                    this.treeView1.Nodes.Add(new_child);
                }
                else
                {
                    //    string text = (node.Value != null ? node.Value :
                    //(node.Attributes != null &&
                    //node.Attributes.Count > 0) ?
                    //node.Attributes[0].Value : node.Name);

                    string text = node.Name;

                    new_child = new TreeNode(text);

                    nodes.Add(new_child);

                    if (node.Attributes != null)
                    {
                        foreach (var item in node.Attributes)
                        {
                        }
                    }
                }

                populateTreeControl(node, new_child.Nodes);
            }
        }
    }
}